// DialogView.cpp : implementation of the CDialogView class
//

#include "stdafx.h"
#include "DialogTool.h"

#include "DialogDoc.h"
#include "DialogView.h"
#include "MemDC.h"
#include "ChildFrm.h"
#include "LeftView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define VIEW_ORG_X	16
#define VIEW_ORG_Y	16

/////////////////////////////////////////////////////////////////////////////
// CDialogView

IMPLEMENT_DYNCREATE(CDialogView, CScrollView)

BEGIN_MESSAGE_MAP(CDialogView, CScrollView)
	//{{AFX_MSG_MAP(CDialogView)
	ON_WM_TIMER()
	ON_WM_ERASEBKGND()
	ON_WM_DESTROY()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDialogView construction/destruction

CDialogView::CDialogView()
{
	m_pSelDlg = NULL;
	m_pItem = NULL;
	m_nTimer = 0;
}

CDialogView::~CDialogView()
{
}

BOOL CDialogView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CScrollView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CDialogView drawing

void CDialogView::OnDraw(CDC* pDC)
{
	CDialogDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	POINT ptOrg;
	ptOrg.x = VIEW_ORG_X;
	ptOrg.y = VIEW_ORG_Y;

	CSize tSize = GetTotalSize();

	RECT rect;
	GetClientRect(&rect);
	rect.left = 0;
	rect.top = 0;
	if(rect.right < tSize.cx)
		rect.right = tSize.cx;
	if(rect.bottom < tSize.cy)
		rect.bottom = tSize.cy;


	CMemDC* pMemDC = new CMemDC(pDC, rect);
	pMemDC->EraseBkgnd(m_brBkgnd, m_brHatch, VIEW_ORG_X, VIEW_ORG_Y);


	if(m_pSelDlg != NULL)
	{
		list<DITEM*>::iterator it;
		it = m_pSelDlg->ItemList.begin();
		while(it != m_pSelDlg->ItemList.end())
		{
			DITEM* pItem = *it++;

			if(pItem->pCtrl)
			{
				int x = ptOrg.x + pItem->nPosX;
				int y = ptOrg.y + pItem->nPosY;
				pItem->pCtrl->OnDraw(pMemDC, x, y);
			}
		}
	}
	
	delete pMemDC;
	
}

void CDialogView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;
	sizeTotal.cx = sizeTotal.cy = 300;
	SetScrollSizes(MM_TEXT, sizeTotal);

	VERIFY(m_brBkgnd.CreateSolidBrush(RGB(128, 128, 128)));
	VERIFY(m_brHatch.CreateHatchBrush(HS_CROSS, RGB(191, 191, 191)));

	//m_nTimer = SetTimer(101, 333, NULL);

}

/////////////////////////////////////////////////////////////////////////////
// CDialogView diagnostics

#ifdef _DEBUG
void CDialogView::AssertValid() const
{
	CScrollView::AssertValid();
}

void CDialogView::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}

CDialogDoc* CDialogView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDialogDoc)));
	return (CDialogDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CDialogView message handlers

void CDialogView::OnTimer(UINT nIDEvent) 
{
	if(m_pSelDlg)	
	{
		list<DITEM*>::iterator it;
		it = m_pSelDlg->ItemList.begin();
		while(it != m_pSelDlg->ItemList.end())
		{
			DITEM* pItem = *it++;

			if(pItem->pCtrl)
			{
				pItem->pCtrl->OnUpdate();
			}
		}
		Invalidate();
	}
	
	CScrollView::OnTimer(nIDEvent);
}

BOOL CDialogView::OnEraseBkgnd(CDC* pDC) 
{
	return 1;
}

void CDialogView::OnDestroy() 
{
	CScrollView::OnDestroy();
	if(m_nTimer)	
	{
		KillTimer(m_nTimer);
		m_nTimer = 0;
	}
}

void CDialogView::OnMouseMove(UINT nFlags, CPoint point) 
{
	if(nFlags & MK_LBUTTON)
	{
		if(m_pItem != NULL)
		{
			CPoint pt = point;
			pt.x -= VIEW_ORG_X;
			pt.y -= VIEW_ORG_Y;
			
			if(m_bDragging == FALSE)
			{
				RECT rect = {0, 0, 0, 0};
				m_pItem->GetRect(rect);

				m_ptOffset.x = pt.x - rect.left;
				m_ptOffset.y = pt.y - rect.top;

				m_bDragging = TRUE;
			}

			if(m_bDragging)
			{
				pt.x = pt.x - m_ptOffset.x;
				pt.y = pt.y - m_ptOffset.y;
				
				m_pItem->nPosX = pt.x > 0 ? pt.x : 0;
				m_pItem->nPosY = pt.y > 0 ? pt.y : 0;
				Invalidate();
			}
		}
	}
	else
	{
		m_bDragging = FALSE;
		m_pItem = NULL;
	}
	CScrollView::OnMouseMove(nFlags, point);
}

void CDialogView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	int x = point.x - VIEW_ORG_X;
	int y = point.y - VIEW_ORG_Y;

	m_pItem = HitSelect(x, y);
	
	CScrollView::OnLButtonDown(nFlags, point);
}

CDialogDoc::DITEM* CDialogView::HitSelect(int x, int y)
{
	if(m_pSelDlg)
	{
		list<DITEM*>::const_iterator it;
		it = m_pSelDlg->ItemList.end();
		while(it != m_pSelDlg->ItemList.begin())
		{
			it--;
			DITEM* pItem = *it;

			if(pItem->pCtrl)
			{
				int xx = x - pItem->nPosX;
				int yy = y - pItem->nPosY;
				if(pItem->pCtrl->HitTest(xx, yy))
				{
					return pItem;
				}
			}
		}
		
	}
	return NULL;
}

void CDialogView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	m_pItem = NULL;
	
	CScrollView::OnLButtonUp(nFlags, point);
}
